module repository.SystemConfigRepository;

import hunt.framework;
import hunt.logging;
import std.conv : to;
import std.array;
import bootstrap.DatabaseBootstrap;
import model;
import message.MsgDefine;
import core.stdc.time;

class SystemConfigRepository : EntityRepository!( SystemConfig , string){

  this()
  {
    super(defaultEntityManager());
  }

  string[string] getAll()
  {
    scope(exit)
    {
      _manager.close();
    }
    string[string] map;

    SystemConfig[] array =  _manager.createQuery!(SystemConfig)("select * from SystemConfig").getResultList();

    foreach(SystemConfig obj ; array)
    {
        map[obj.key] = obj.value;
    }
    return map;
  }

  void upDataId(string newId)
  {
    scope(exit)
    {
      _manager.close();
    }
    auto update = _manager.createQuery!(SystemConfig)("UPDATE SystemConfig u SET u.value = :value WHERE u.key = :key");
    update.setParameter("value",newId);
    update.setParameter("key","next-dbid");
    try{
      update.exec();
    }
    catch(Exception e)
    {
      logError("upDataId error : %s",e.msg);
    }
  }
}

